package com.logistics.User;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.json.annotations.JSON;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.DAO.User;
import com.opensymphony.xwork2.ActionSupport;

public class Displayer extends ActionSupport implements ServletRequestAware{

	private static final long serialVersionUID = 1L;
	/**
	 * @title Manager.java
	 * @package com.logistics.User
	 * @author ufo
	 */

	private Map<String,String> result=new HashMap<String,String>();
	private HttpServletRequest request;
	private String[] id;

	protected Configuration conf=new Configuration().configure();
	protected SessionFactory sf=conf.buildSessionFactory();
	protected Session hibernateSess=sf.openSession();
	protected Transaction tx = hibernateSess.beginTransaction();
	protected User user;

	public String display(){
		if(isOnline()){
			addSessionToUser();
			result.put("id", user.getId());
			result.put("userName", user.getUserName());
			result.put("Flag", user.getFlag()==1?"true":"false");
			result.put("flag", "true");
			return SUCCESS;
		}
		result.put("flag", "false");
		return SUCCESS;
	}
	private void addSessionToUser(){
		user=(User) hibernateSess.load(User.class, (Serializable) request.getSession().getAttribute("userId"));
	}
	public boolean isOnline(){
		if(request.getSession().getAttribute("userId")!=null)
			return true;
		else 
			return false;
	}
	@Override
	public void setServletRequest(HttpServletRequest request) {
		// TODO 自动生成的方法存根
		this.setRequest(request);
	}
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
@JSON(name="result")
	public Map<String,String> getResult() {
		return result;
	}
	public void setResult(Map<String,String> result) {
		this.result = result;
	}
	public String[] getId() {
		return id;
	}
	public void setId(String[] id) {
		this.id = id;
	}

}
